//
//  XJ_UnsubmissionPlanListViewController.m
//  XunJieProject
//
//  Created by 薛子 on 2018/6/12.
//  Copyright © 2018年 Soowin. All rights reserved.
//

#import "XJ_UnsubmissionPlanListViewController.h"
#import "XJ_UnsubmissPlanListCell.h"
#import "XJ_ConfirmationMarginViewController.h"
#import "XJ_PlanListModel.h"
#import "BC_KeFuAlextView.h"
@interface XJ_UnsubmissionPlanListViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    
    NSString * planID;
    UIButton * submissionPlanBtn;
    
}
@property(nonatomic, strong)UITableView * listTable;
@property(nonatomic, strong)NSMutableArray * planListArray;
@end

@implementation XJ_UnsubmissionPlanListViewController

- (NSMutableArray *)planListArray{
    
    if (_planListArray == nil) {
        
        _planListArray = [NSMutableArray array];
    }
    
    return _planListArray;
}
- (UITableView *)listTable{
    
    if (_listTable == nil) {
        
        _listTable = [[UITableView alloc]initWithFrame:CGRectMake(15, SafeAreaTopHeight + 10, SCREEN_WIDTH - 30, SCREEN_HEIGHT - SafeAreaTopHeight - 100) style:UITableViewStylePlain];
        _listTable.delegate = self;
        _listTable.dataSource = self;
        _listTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        _listTable.layer.cornerRadius = 8;
        
    }
    
    return _listTable;
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    // 禁用返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    // 开启返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.nav_titleLabel.text = @"未提交计划列表";
    
    [self requestSubmissionPlanData];
    
    self.nav_rightButton.hidden = NO;
    [self.nav_rightButton setTitle:@"清空" forState:UIControlStateNormal];
    [self.nav_rightButton addTarget:self action:@selector(emptyListAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.listTable];
    
    submissionPlanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    submissionPlanBtn.frame = CGRectMake(15, SCREEN_HEIGHT - 45 - 17, SCREEN_WIDTH - 30, 45);
    [submissionPlanBtn setTitle:@"提交计划" forState:UIControlStateNormal];
    [submissionPlanBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [submissionPlanBtn setBackgroundColor:[UIColor colorWithHexString:@"fa832f"]];
    [submissionPlanBtn addTarget:self action:@selector(submissionPlanAction:) forControlEvents:UIControlEventTouchUpInside];
    submissionPlanBtn.titleLabel.font =[UIFont systemFontOfSize:15.0];
    submissionPlanBtn.layer.cornerRadius =4;
    submissionPlanBtn.layer.masksToBounds =YES;
    [self.view addSubview:submissionPlanBtn];
    
    // Do any additional setup after loading the view.
}

- (void)requestSubmissionPlanData{
    
    NSUserDefaults * db = [NSUserDefaults standardUserDefaults];
    NSString * token = [db objectForKey:@"token"];
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setValue:IDENTIFIER forKey:@"equipmentNo"];
    [dic setValue:APPVERSION forKey:@"appVersion"];
    [dic setValue:STSTERMVERSION forKey:@"systemVersion"];
    [dic setValue:USERID forKey:@"userId"];
    
    if (token!=nil &&![token isEqualToString:@""]) {
        
        [dic setValue:token forKey:@"token"];
        
    }
    
    if (_planID) {
        
        [dic setValue:_planID forKey:@"planId"];
        
    }else
    {
        [dic setValue:_cardModel.planId forKey:@"planId"];
    }
    
    [XJ_Utils signPost:@"plan/unsubmitList?" parameters:dic success:^(NSInteger recode, id jsonObject) {
        
        [self.planListArray removeAllObjects];
        
        NSDictionary * dataDic = [jsonObject objectForKey:@"data"];
        
        self->planID = [dataDic objectForKey:@"planId"];
        
        NSArray *planListArr = [dataDic objectForKey:@"planList"];
        
        if (planListArr.count > 0) {
            
            self.listTable.hidden = NO;
            self->submissionPlanBtn.hidden = NO;
            
            for (NSDictionary * dic in planListArr) {
                
                XJ_PlanListModel * model = [XJ_PlanListModel mj_objectWithKeyValues:dic];
                
                [self.planListArray addObject:model];
            }
            
            [self.listTable reloadData];
            
        }else{
            
            self.listTable.hidden = YES;
            self->submissionPlanBtn.hidden = YES;
            
        }
        
        
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
}
- (void)submissionPlanAction:(UIButton *)sender{
    
    NSUserDefaults * db = [NSUserDefaults standardUserDefaults];
    NSString * token = [db objectForKey:@"token"];
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setValue:IDENTIFIER forKey:@"equipmentNo"];
    [dic setValue:APPVERSION forKey:@"appVersion"];
    [dic setValue:STSTERMVERSION forKey:@"systemVersion"];
    [dic setValue:USERID forKey:@"userId"];
    
    if (token!=nil &&![token isEqualToString:@""]) {
        
        [dic setValue:token forKey:@"token"];
        
    }
    
    
    
    if (_planID) {
        
        [dic setValue:_planID forKey:@"planId"];
        
    }else
    {
        [dic setValue:_cardModel.planId forKey:@"planId"];
    }
    
    [XJ_Utils signPost:@"plan/preAdd?" parameters:dic success:^(NSInteger recode, id jsonObject) {
        
        NSDictionary * dataDic = [jsonObject objectForKey:@"data"];
        
        XJ_PlanPreAddModel * preModel;
        
        if (dataDic.count > 0) {
            
            preModel = [XJ_PlanPreAddModel mj_objectWithKeyValues:dataDic];
        }
        
        
        XJ_ConfirmationMarginViewController * VC = [[XJ_ConfirmationMarginViewController alloc]init];
        VC.model = preModel;
        [self.navigationController pushViewController:VC animated:YES];
        
       
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
  
}
#pragma tableDelegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.planListArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XJ_UnsubmissPlanListCell * cell = [tableView dequeueReusableCellWithIdentifier:@"XJ_UnsubmissPlanListCell"];
    if (cell == nil) {
        
        cell = [[[NSBundle mainBundle]loadNibNamed:@"XJ_UnsubmissPlanListCell" owner:self options:nil]objectAtIndex:0];
        
    }
    
    XJ_PlanListModel * planModel = [self.planListArray objectAtIndex:indexPath.row];
    
    
    cell.planTimeLabel.text = planModel.planDetailDate;
    cell.planTypeLabel.text = planModel.planDetailTypeDes;
    cell.planMoneyLabel.text = planModel.planDetailMoney;
    
    return cell;
}

////清空计划
- (void)emptyListAction:(UIButton *)sender{
    
    
    BC_KeFuAlextView *alext =[[BC_KeFuAlextView alloc]initWithMessage:@"提示" sureBtn:@"是" cancleBtn:@"否" phone:@"清空计划吗？" delete:YES];
    alext.resultIndex = ^(NSInteger index) {
        
        [self loadPlanClean];
        
        
    };
    [alext showXLAlertView];
    
   
    
}

#pragma mark---清空计划
-(void)loadPlanClean{
    
    
    
    NSUserDefaults * db = [NSUserDefaults standardUserDefaults];
    NSString * token = [db objectForKey:@"token"];
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setValue:IDENTIFIER forKey:@"equipmentNo"];
    [dic setValue:APPVERSION forKey:@"appVersion"];
    [dic setValue:STSTERMVERSION forKey:@"systemVersion"];
    [dic setValue:USERID forKey:@"userId"];
    
    if (token!=nil &&![token isEqualToString:@""]) {
        
        [dic setValue:token forKey:@"token"];
        
    }
    
    if (_planID) {
        
        [dic setValue:_planID forKey:@"planId"];
    }else
    {
        [dic setValue:_cardModel.planId forKey:@"planId"];
    }
    
    
    
    [XJ_Utils signPost:@"plan/clean?" parameters:dic success:^(NSInteger recode, id jsonObject) {
        
        ZDXAlertView *alext =[[ZDXAlertView alloc]initWithMessage:@"成功" image:@"pop_msg_tip_xs"];
        [alext showXLAlertView];
        [self.navigationController popToRootViewControllerAnimated:YES];
        
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
    
    
}
- (void)backButtonClick{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
